package com.muqi.app.user.widget;

import java.util.Calendar;

import com.muqi.app.project.utils.ScreenUtils;
import com.muqi.app.project.utils.TimeMangerUtil;
import com.muqi.app.qlearn.student.R;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.PopupWindow.OnDismissListener;

public class DatePickerWindow implements OnDismissListener,OnClickListener {
	private Activity mActivity;
	private PopupWindow window;
	private ImageButton button_cancel,button_ok;
	private TextView dialog_title;
	private String strTime = "";
	private OnClickConfirmListener confirmListener = null;
	
	public DatePickerWindow(Activity mActivity,String title,OnClickConfirmListener confirmListener){
		this.mActivity = mActivity;
		this.confirmListener = confirmListener;
		window = new PopupWindow(mActivity);
		window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		View contentView = LayoutInflater.from(mActivity).inflate(R.layout.dialog_date, null);
		dialog_title = (TextView) contentView.findViewById(R.id.dialog_title);
		dialog_title.setText(title);
		// 点击事件处理
		button_ok = (ImageButton) contentView.findViewById(R.id.btn_confirm);
		button_ok.setOnClickListener(this);
		button_cancel = (ImageButton) contentView.findViewById(R.id.btn_cancel);
		button_cancel.setOnClickListener(this);
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int monthOfYear = calendar.get(Calendar.MONTH);
		int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
		DatePicker datePicker = (DatePicker) contentView.findViewById(R.id.dialog_datePicker);
		datePicker.init(year, monthOfYear, dayOfMonth, new OnDateChangedListener() {
			@Override
			public void onDateChanged(DatePicker view, int year,
					int monthOfYear, int dayOfMonth) {
				StringBuffer sb = new StringBuffer();
				sb.append(String.format("%d-%02d-%02d", year, monthOfYear + 1, dayOfMonth));
				strTime = sb.toString();
			}
		});
		window.setContentView(contentView);
		window.setAnimationStyle(R.style.ActionSheetAnimation);
		window.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);           
		window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
		window.setWidth(ScreenUtils.getScreenW(mActivity));
		window.setHeight(ScreenUtils.getScreenH(mActivity)/3);
		window.setFocusable(true); //默认为false,如果不设置为true，PopupWindow里面是获取不到焦点的，那么如果PopupWindow里面有输入框等的话就无法输入。
		window.setTouchable(true); // 设置PopupWindow可触摸
		window.setOutsideTouchable(true);
		window.setOnDismissListener(this);
		backgroundAlpha(0.5f);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_confirm:
			if(TextUtils.isEmpty(strTime)){
				strTime = TimeMangerUtil.getNowDate();
			}
			confirmListener.onClickOkDate(strTime);
			dissmiss();
			break;
		case R.id.btn_cancel:
			dissmiss();
			break;
		default:
			break;
		}
	}
	
	public interface OnClickConfirmListener{
		/** 设置点击确认按钮时监听接口 */
		public void onClickOkDate(String strDate);
	}

	@Override
	public void onDismiss() {
		backgroundAlpha(1f);
	}
	
	/**弹窗显示的位置*/  
	public void showAsDropDown(View parent){
		window.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
		window.setFocusable(true);
		window.setOutsideTouchable(true);
		window.update();
	}
	
	/**消除弹窗*/
	public void dissmiss(){
		window.dismiss();
	}
	
	public void backgroundAlpha(float bgAlpha) {  
        WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();  
        lp.alpha = bgAlpha; //0.0-1.0  
        mActivity.getWindow().setAttributes(lp);  
    }
}
